package cn.cs.service.impl;

import cn.cs.client.AgentClient;
import cn.cs.client.AgentSettlementClient;
import cn.cs.client.CsPersonClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.Agent;
import cn.cs.pojo.AgentSettlement;
import cn.cs.pojo.CsPerson;
import cn.cs.service.AgentSettlementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AgentSettlementServiceImpl implements AgentSettlementService {

    @Resource
    private AgentSettlementClient agentSettlementClient;

    @Resource
    private AgentClient agentClient;

    @Resource
    private CsPersonClient personClient;


    public AgentSettlement getAgentSettlementById(String id) throws Exception {
        return agentSettlementClient.getAgentSettlementById(id);
    }

    public List<AgentSettlement> getAgentSettlementListByMap(Map<String, Object> param) throws Exception {
        return agentSettlementClient.getAgentSettlementListByMap(param);
    }

    public Integer getAgentSettlementCountByMap(Map<String, Object> param) throws Exception {
        return agentSettlementClient.getAgentSettlementCountByMap(param);
    }

    public Integer addAgentSettlement(AgentSettlement agentSettlement) throws Exception {
        return agentSettlementClient.addAgentSettlement(agentSettlement);
    }

    public Integer modifyAgentSettlement(AgentSettlement agentSettlement) throws Exception {
        return agentSettlementClient.modifyAgentSettlement(agentSettlement);
    }

    public Integer deleteAgentSettlementById(String id) throws Exception {
        return agentSettlementClient.deleteAgentSettlementById(id);
    }

    public Page<AgentSettlement> queryAgentSettlementPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = agentSettlementClient.getAgentSettlementCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<AgentSettlement> agentSettlementList = agentSettlementClient.getAgentSettlementListByMap(param);
        for(AgentSettlement a:agentSettlementList){
            String agentId = a.getAgentId();
            Agent agent = agentClient.getAgentById(agentId);
            if(EmptyUtils.isNotEmpty(agent)){
                a.setName(agent.getName());
                a.setPhone(agent.getPhone());
                a.setPerson(agent.getPerson());
            }

            String personId = a.getPersonId();
            CsPerson person = personClient.findPersonById(personId);
            if(EmptyUtils.isNotEmpty(person)){
                a.setPerson_name(person.getPersonName());
            }
        }
        page.setRows(agentSettlementList);
        return page;
    }

}
